package com.example.wxai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.wxai.mapper.CommentMapper;
import com.example.wxai.mapper.EssayMapper;
import com.example.wxai.mapper.LikesMapper;
import com.example.wxai.mapper.UserMapper;
import com.example.wxai.pijo.dto.CommentDto;
import com.example.wxai.pijo.entity.Comment;
import com.example.wxai.pijo.entity.Essay;
import com.example.wxai.pijo.entity.Likes;
import com.example.wxai.pijo.entity.User;
import com.example.wxai.pijo.vo.CommentVo;
import com.example.wxai.result.PageResult;
import com.example.wxai.service.ICommentService;
import com.example.wxai.utils.BeanCopyUtils;
import com.example.wxai.utils.DatetimeUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-15
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Resource
    private CommentMapper commentMapper;
    @Resource
    private LikesMapper likesMapper;
    @Resource
    private EssayMapper essayMapper;
    @Resource
    private UserMapper userMapper;
    @Transactional
    @Override
    public void addComment(CommentDto commentDto) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDto,comment);
        comment.setCommentLikeCount(0);
        comment.setIsDelete(false);
        comment.setCreatTime(DatetimeUtil.getTheTimeInMinutes());
        commentMapper.insert(comment);

        //文章评论数加1
        UpdateWrapper<Essay> updateWrapper=new UpdateWrapper();
        updateWrapper.eq("essay_id",commentDto.getEssayId()).setSql("essay_reviews=essay_reviews+1");
        essayMapper.update(updateWrapper);

        //我的评论数加1
        UpdateWrapper<User> userUpdateWrapper=new UpdateWrapper<>();
        userUpdateWrapper.eq("user_id",commentDto.getUserId()).setSql("reviews=reviews+1");
        userMapper.update(userUpdateWrapper);

    }

    @Override
    public PageResult<CommentVo> getPageComment(String essayId, Integer pageNo, String userId) {
        Page<Comment> page= Page.of(pageNo,10);
        //查询十条顶级评论
        lambdaQuery().isNull(Comment::getRootCommentId).eq(Comment::getEssayId,essayId).page(page);
        PageResult<CommentVo> commentVoPageResult=new PageResult<>();
        commentVoPageResult.setList(BeanCopyUtils.copyListProperties(page.getRecords(),CommentVo::new));
        commentVoPageResult.setTotal(page.getTotal());
        //子评论前三条
        commentVoPageResult.getList().forEach(e->{
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("root_comment_id",e.getCommentId());
            queryWrapper.last("limit 3");
            List list = commentMapper.selectList(queryWrapper);
            e.setSubCommentList(list);
            //为每条顶级评论计算子评论数量
            e.setSubCommentCount(lambdaQuery().eq(Comment::getRootCommentId,e.getCommentId()).count());
            //查询每条顶级评论是否被我点赞
           QueryWrapper<Likes> likesQueryWrapper=new QueryWrapper<>();
           likesQueryWrapper.eq("user_id",userId).eq("to_comment_id",e.getCommentId());
            boolean exists = likesMapper.exists(likesQueryWrapper);
            e.setIsCommentLikes(exists);
        });


        return commentVoPageResult;
    }
}
